package manager.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebFilter("/*")
public class CharcterFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpreq = (HttpServletRequest) servletRequest;
        HttpServletResponse httpresp = (HttpServletResponse) servletResponse;
        String requesttype = httpreq.getHeader("x-requested-with");
        if ("XMLHttpRequest".equals(requesttype)){
            httpresp.setHeader("Content-Type","text/json");
        }
        Enumeration e = httpreq.getHeaderNames();
        while (e.hasMoreElements()){
            Object name = e.nextElement();
            String v = httpreq.getHeader(name.toString());
        }
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
